# coding=utf-8
import re
import hashlib
from Config.config import client



def md5(string):
    hl = hashlib.md5()
    hl.update(string.encode(encoding='utf-8'))
    return hl.hexdigest()[8:-8]


def search(lock, response):
    items = []
    for i in lock:
        item = {}
        re_string = '<glyph glyph-name="{}" unicode=".*?;" d="(.*?)"'.format(i)
        datas = re.findall(re_string, response)
        data = datas[0] if len(datas) != 0 else None
        item[i] = md5(str(data)) if data is not None else i
        items.append(item)
    return items


def match(item):
    key, value = list(item.keys())[0], list(item.values())[0]
    response = client.find({'_id': value})
    try:
        string = response[0]['string']
        return string
    except:
        return key


if __name__ == '__main__':
    print(client.find({'_id': '8d6ace9ecd686559'})[0])
